﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Package
{
    public partial class PackageUseRecordListView : UserControl, IView
    {
        #region fields
        Promise _promise = new Promise();
        private int _pageIndex;
        private int _pageCount;
        #endregion

        public PackageUseRecordListView()
        {
            InitializeComponent();
            ctlGrid.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", new Action<object, object>((o1, o2) =>
            {
                _pageIndex = 0;
                LoadPackageInstanceItemChangedRecordList();
            })));
        }

        public void Active(ViewActiveArgs args)
        {
            _promise.Then(LoadPackageInstanceItemChangedRecordList)
                .Run();
        }

        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs != null && args.ClosedArgs.Result)
            {
                if (args.ClosedArgs.View is Pickers.VCustomerAccuratelyPicker)
                {
                    WebRef.VCustomer customer = args.ClosedArgs.Data as WebRef.VCustomer;
                    ctlEditCustomer.Text = customer.Name;
                    ctlEditCustomer.Tag = customer;
                }
            }
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("套餐使用情况");
        }
        #endregion

        #region methods
        private void LoadPackageInstanceItemChangedRecordList()
        {
            this.ShowMask();
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VPackageInstanceItemChangedRecord> result = new Service.QueryService().QueryPackageInstanceItemChangedRecordPagedList(GetCustomerId(), _pageIndex * GetLimit(), GetLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        ctlGrid.Paginate<WebRef.VPackageInstanceItemChangedRecord>(result, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }

        public int GetLimit()
        {
            return 25;
        }

        public Guid? GetCustomerId()
        {
            WebRef.VCustomer customer = ctlEditCustomer.Tag as WebRef.VCustomer;
            if (customer == null)
            {
                return null;
            }
            return customer.Id;
        }
        #endregion

        #region Action
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
        }
        private void OnEditCustomerClick(object sender, EventArgs e)
        {
            UIContext.Current.ViewManager.PopupView(new Pickers.VCustomerAccuratelyPicker(), new PopupViewArgs());
        }
        private void ctlBtnClear_Click(object sender, EventArgs e)
        {
            ctlEditCustomer.Tag = null;
            ctlEditCustomer.Text = string.Empty;
        }
        #endregion
    }
}
